﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Momentum.Static;
using Momentum.Types;

namespace Momentum
{
    public class mWorld
    {
        mTileGrid groundLayer;
        //mTileGrid detailLayer;
        //mTileGrid rockLayer;
        mSpriteSheet groundSheet;
        const int numX = 6;
        const int numY = 6;
        Random rand;
        public List<mObject> Objects;
        public List<Vector2> Explosions;

        public mWorld()
        {
            rand = new Random();
            Objects = new List<mObject>();
            Explosions = new List<Vector2>();
            mStaticWorld.Friction = 0.1f;
            mStaticWorld.Size = new Vector2(numX * 510, numY * 510);
        }

        public void LoadContent(ContentManager content, GraphicsDeviceManager graphics)
        {
            Texture2D groundTexture = content.Load<Texture2D>("ground");
            groundSheet = new mSpriteSheet(groundTexture);
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.Base,
                new Rectangle(0, 0, 510, 510));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.Detail1,
                new Rectangle(514, 0, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.Detail2,
                new Rectangle(769, 0, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.Detail3,
                new Rectangle(514, 256, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.Detail4,
                new Rectangle(769, 256, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.SoftDetail1,
                new Rectangle(514, 514, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.SoftDetail2,
                new Rectangle(769, 514, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.SoftDetail3,
                new Rectangle(514, 769, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.SoftDetail4,
                new Rectangle(769, 769, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.Rocks1,
                new Rectangle(0, 514, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.Rocks2,
                new Rectangle(256, 514, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.Rocks3,
                new Rectangle(0, 769, 255, 255));
            groundSheet.AddSourceSprite((int)mTileGrid.TileName.Rocks4,
                new Rectangle(256, 769, 255, 255));
            groundLayer = new mTileGrid(510, 510, numX, numY, Vector2.Zero, groundSheet, graphics);
            for (int i = 0; i < numX; i++)
            {
                for (int j = 0; j < numY; j++)
                {
                    groundLayer.SetTile(i, j, 1);
                }
            }
            /*detailLayer = new mTileGrid(255, 255, numX * 2 - 1, numY * 2 - 1,
                new Vector2(127, 127), groundSheet, graphics);
            rockLayer = new mTileGrid(255, 255, numX * 2 - 1, numY * 2 - 1,
                new Vector2(0, 0), groundSheet, graphics);
            for (int i = 0; i < numX * 2 - 1; i++)
            {
                for (int j = 0; j < numY * 2 - 1; j++)
                {
                    switch (rand.Next(20))
                    {
                        case 0:
                            detailLayer.SetTile(i, j, (int)mTileGrid.TileName.Detail1);
                            break;
                        case 1:
                            detailLayer.SetTile(i, j, (int)mTileGrid.TileName.Detail2);
                            break;
                        case 2:
                            detailLayer.SetTile(i, j, (int)mTileGrid.TileName.Detail3);
                            break;
                        case 3:
                            detailLayer.SetTile(i, j, (int)mTileGrid.TileName.Detail4);
                            break;
                        case 4:
                        case 5:
                            detailLayer.SetTile(i, j, (int)mTileGrid.TileName.SoftDetail1);
                            break;
                        case 6:
                        case 7:
                            detailLayer.SetTile(i, j, (int)mTileGrid.TileName.SoftDetail2);
                            break;
                        case 8:
                        case 9:
                            detailLayer.SetTile(i, j, (int)mTileGrid.TileName.SoftDetail3);
                            break;
                        case 10:
                        case 11:
                            detailLayer.SetTile(i, j, (int)mTileGrid.TileName.SoftDetail4);
                            break;
                    }
                }
            }*/
        }

        public void Update()
        {
            foreach (mObject obj in Objects)
            {
                obj.Move(mStaticWorld.Friction);
                obj.UpdatePosition();
            }
        }

        public void SetCamera(mCamera2D camera)
        {
            groundLayer.CameraPosition = camera.Position;
            groundLayer.CameraRotation = camera.Rotation;
            groundLayer.CameraZoom = camera.Zoom;
            /*detailLayer.CameraPosition = camera.Position;
            detailLayer.CameraRotation = camera.Rotation;
            detailLayer.CameraZoom = camera.Zoom;
            rockLayer.CameraPosition = camera.Position;
            rockLayer.CameraRotation = camera.Rotation;
            rockLayer.CameraZoom = camera.Zoom;*/
        }

        public void DrawBackground(SpriteBatch spriteBatch)
        {
            groundLayer.Draw(spriteBatch);
            //detailLayer.Draw(spriteBatch);
            //rockLayer.Draw(spriteBatch);
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 cameraPosition)
        {
            foreach (mObject obj in Objects)
            {
                obj.CameraPosition = cameraPosition;
                obj.SetPositionToCircularWorld();
                obj.Draw(spriteBatch);
            }
        }
    }
}
